package com.hongtu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hongtu.dao.AccountInfoDao;
import com.hongtu.model.AccountChangeEvent;
import com.hongtu.service.AccountInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class AccountInfoServiceImpl implements AccountInfoService {


    @Autowired
    AccountInfoDao accountInfoDao;


    @Override
    public void addAccountInfoBalance(AccountChangeEvent accountChangeEvent) {
        log.info("bank2更新本地账号，账号：{},金额： {}", accountChangeEvent.getAccountNo(), accountChangeEvent.getAmount());

        String txNo = accountChangeEvent.getTxNo();
        if (accountInfoDao.isExistTx(txNo) <= 0) {
            accountInfoDao.updateAccountBalance(accountChangeEvent.getAccountNo(), accountChangeEvent.getAmount());
            accountInfoDao.addTx(txNo);
            log.info("更新本地事务执行成功，本次事务号: {}", accountChangeEvent.getTxNo());
        } else {
            log.info("更新本地事务执行失败，本次事务号: {}", accountChangeEvent.getTxNo());
        }

    }
}
